﻿/*
 * 页面描述：议价设置
 * 创建时间：2009/8/25
 * 创建人：周司徒
 * 
 * 修改人：
 * 修改时间：
 * 修改内容：
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.BidSystem.Model;
using System.Drawing;
using System.Configuration;
using System.Data;

public partial class Manage_BargainSetting : System.Web.UI.Page, IAccess
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CurrentAccount.CheckSessionTimeOut(this);
        if (!IsPostBack)
        {

            BindData();
        }
    }

    private void BindData()
    {
        var pc = BLL_Factory.IBLL_Manage.GetProjectConfig(CurrentAccount.ProjectId);
        ViewState["BargainSwitch"] = pc.BargainSwitch ?? false;
        if (pc.BargainSwitch ?? false)
        {
            lblinfo.Text = "议价开关当前状态：开启";
            lblinfo.ForeColor = Color.Green;
            btnBargainSwitch.Text = "关闭议价开关";
        }
        else
        {
            lblinfo.Text = "议价开关当前状态：关闭";
            lblinfo.ForeColor = Color.Red;
            btnBargainSwitch.Text = "开启议价开关";

        }
        txtBarBeginTime.Text = pc.BargainStartTime == null ? "" : pc.BargainStartTime.Value.ToString("yyyy/MM/dd HH:mm:ss");
        txtBarEndTime.Text = pc.BargainEndTime == null ? "" : pc.BargainEndTime.Value.ToString("yyyy/MM/dd HH:mm:ss");
        txtBarBeginTime2.Text = pc.BargainStartTime == null ? "" : pc.SecondBargainStartTime.Value.ToString("yyyy/MM/dd HH:mm:ss");
        txtBarEndTime2.Text = pc.BargainEndTime == null ? "" : pc.SecondBargainEndTime.Value.ToString("yyyy/MM/dd HH:mm:ss");
        txtBarDecBeginTime.Text = pc.DecStartTime == null ? "" : pc.DecStartTime.Value.ToString("yyyy/MM/dd HH:mm:ss");
        txtBarDecEndTime.Text = pc.DecEndTime == null ? "" : pc.DecEndTime.Value.ToString("yyyy/MM/dd HH:mm:ss");
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        ProjectConfig pc = new ProjectConfig()
        {
            ProjectId = CurrentAccount.ProjectId,
            BargainStartTime = new HZ.Common.ConvertHelper(txtBarBeginTime.Text).ToDateTime,
            BargainEndTime = new HZ.Common.ConvertHelper(txtBarEndTime.Text).ToDateTime,
            SecondBargainStartTime=new HZ.Common.ConvertHelper(txtBarBeginTime2.Text).ToDateTime,
            SecondBargainEndTime = new HZ.Common.ConvertHelper(txtBarEndTime2.Text).ToDateTime,
            DecStartTime = new HZ.Common.ConvertHelper(txtBarDecBeginTime.Text).ToDateTime,
            DecEndTime = new HZ.Common.ConvertHelper(txtBarDecEndTime.Text).ToDateTime
        };
        BLL_Factory.IBLL_Manage.SetProjectConfig(pc);
    }
    protected void btnBargainSwitch_Click(object sender, EventArgs e)
    {
        bool sw = (bool)ViewState["BargainSwitch"];
        var pc = new ProjectConfig() { ProjectId = CurrentAccount.ProjectId };
        if (sw)
        {
            pc.BargainSwitch = false;

        }
        else
        {
            pc.BargainSwitch = true;
        }

        BLL_Factory.IBLL_Manage.SetProjectConfig(pc);
        BindData();
    }
    #region IAccess Members

    IList<EnumAccess> IAccess.AccessList
    {
        get
        {
            var list = new List<EnumAccess>();
            list.Add(EnumAccess.Manage);
            return list;
        }

    }

    #endregion
   

    /// <summary>
    /// 同步到本地第一轮议价数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {
            string IsExpertPrice = string.Format(@"select * from PB_BargainExpertPrice where isnull(ExpertPrice1,0)=0");
            DataTable dt = HZ.Data.DbHelperFactory.Create().ExecuteTable(IsExpertPrice);
            if (dt.Rows.Count > 0)
            {
                HZ.Web.MessageBox.Show("存在未议价的产品,请核查数据！");
            }
            else
            {
                string sql = string.Format(@"delete from TB_Bargain;insert into TB_Bargain (ProjectId,BidId,ExpertPrice,CompanyId,Switch)  
 select 1 ProjectId,A.Company_Goods,ExpertPrice,CompanyId_Tb,0 Switch from TB_GoodsBargin A inner join (
select Company_Goods,sum(ExpertPrice1)/5 as ExpertPrice from [dbo].[PB_BargainExpertPrice] group by Company_Goods) B
on A.Company_Goods=B.Company_Goods ");
                int reslust = HZ.Data.DbHelperFactory.Create().ExecuteNonQuery(sql);
                if (reslust > 0)
                {
                    HZ.Common.log4netManager.LogInfo("同步到本地第一轮议价数据", string.Format("同步到本地第一轮议价数据成功：操作人：{0}，操作人IP：{1}，操作时间：{2}", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP, DateTime.Now), null);
                    HZ.Web.MessageBox.Show("同步到本地第一轮议价数据成功");
                }
                else
                {
                    HZ.Common.log4netManager.LogInfo("同步到本地第一轮议价数据", string.Format("同步到本地第一轮议价数据失败：操作人：{0}，操作人IP：{1}，操作时间：{2}", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP, DateTime.Now), null);
                    HZ.Web.MessageBox.Show("同步到本地第一轮议价数据失败");
                }
            }
        }
        catch (Exception)
        {

            HZ.Web.MessageBox.Show("同步到本地第一轮议价数据失败");
        }
    }
    /// <summary>
    /// 同步到外网第一轮议价数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button3_Click(object sender, EventArgs e)
    {
        string tradeconnectionstring = ConfigurationManager.AppSettings["OutNetData"].ToString();
        try
        {
            //先同步本地数据信息 第一轮企业议价表TB_Bargain
            string sql = string.Format(@"delete from openrowset({0}.dbo.TB_Bargain);
                insert into openrowset({0}.dbo.TB_Bargain)(ProjectId,BidId,ExpertPrice,CompanyId,Switch)
                select ProjectId,BidId,ExpertPrice,CompanyId,Switch from TB_Bargain; ", tradeconnectionstring);
            int reslust = HZ.Data.DbHelperFactory.Create().ExecuteNonQuery(sql);
            if (reslust > 0)
            {
                HZ.Common.log4netManager.LogInfo("同步到外网第一轮议价数据", string.Format("同步到外网第一轮议价数据成功：操作人：{0}，操作人IP：{1}，操作时间：{2}", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP, DateTime.Now), null);
                HZ.Web.MessageBox.Show("同步到外网第一轮议价数据成功");
            }
            else
            {
                HZ.Common.log4netManager.LogInfo("同步到外网第一轮议价数据", string.Format("同步到外网第一轮议价数据失败：操作人：{0}，操作人IP：{1}，操作时间：{2}", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP, DateTime.Now), null);
                HZ.Web.MessageBox.Show("同步到外网第一轮议价数据失败");
            }
        }
        catch (Exception)
        {
            HZ.Web.MessageBox.Show("同步到外网第一轮议价数据失败");
        }
    }

    /// <summary>
    /// 同步到内网第一轮议价数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button4_Click(object sender, EventArgs e)
    {
        string tradeconnectionstring = ConfigurationManager.AppSettings["OutNetData"].ToString();
        try
        {
            //先同步本地数据信息 第一轮企业议价表TB_Bargain
            string sql = string.Format(@"update TB_Bargain 
set BargainDecPrice=a.BargainDecPrice,BargainEncryptPrice=a.BargainEncryptPrice,Switch=a.Switch,AcceptDate=a.AcceptDate
from openrowset({0}.dbo.TB_Bargain)as a inner join TB_Bargain b 
on a.BidID=b.BidID ", tradeconnectionstring);
            int reslust = HZ.Data.DbHelperFactory.Create().ExecuteNonQuery(sql);
            if (reslust > 0)
            {
                HZ.Common.log4netManager.LogInfo("同步到内网第一轮议价数据", string.Format("同步到外网第一轮议价数据成功：操作人：{0}，操作人IP：{1}，操作时间：{2}", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP, DateTime.Now), null);
                HZ.Web.MessageBox.Show("同步到外网第一轮议价数据成功");
            }
            else
            {
                HZ.Common.log4netManager.LogInfo("同步到内网第一轮议价数据", string.Format("同步到外网第一轮议价数据失败：操作人：{0}，操作人IP：{1}，操作时间：{2}", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP, DateTime.Now), null);
                HZ.Web.MessageBox.Show("同步到外网第一轮议价数据失败");
            }
        }
        catch (Exception)
        {
            HZ.Web.MessageBox.Show("同步到外网第一轮议价数据失败");
        }
    }

    /// <summary>
    /// 同步到本地第二轮议价数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {

            string IsExpertPrice = string.Format(@"select * from PB_BargainExpertPrice where ExpertPriceNum=2 and  isnull(ExpertPrice2,0)=0");
            DataTable dt = HZ.Data.DbHelperFactory.Create().ExecuteTable(IsExpertPrice);
            if (dt.Rows.Count > 0)
            {
                HZ.Web.MessageBox.Show("存在未议价的产品,请核查数据！");
            }
            else
            {
                string sql = string.Format(@"
delete from TB_Bargain2;
insert into TB_Bargain2 (ProjectId,BidId,ExpertPrice,CompanyId,BargainDecPrice,BargainEncryptPrice,ExpertPrice2,Switch)  
 select 1 ProjectId,A.Company_Goods,Price1,A.CompanyId_Tb,d.BargainDecPrice,d.BargainEncryptPrice,B.ExpertPrice,1 from TB_GoodsBargin A inner join (
select Company_Goods,AVG(ExpertPrice1) Price1,AVG(ExpertPrice2) as ExpertPrice from [dbo].[PB_BargainExpertPrice] where ExpertPriceNum=2 and ExpertState=1 group by Company_Goods) B
on A.Company_Goods=B.Company_Goods inner join TB_Bargain D on b.Company_Goods=d.BidId inner join  (select Company_Goods,count(1) Num1 from PB_BargainExpertPrice where ExpertState=1 group by Company_Goods) C on A.Company_Goods=C.Company_Goods where C.Num1>=3
union all
select 1 ProjectId,A.Company_Goods,Price1,A.CompanyId_Tb,d.BargainDecPrice,d.BargainEncryptPrice,B.ExpertPrice,0 from TB_GoodsBargin A inner join (
select Company_Goods,AVG(ExpertPrice1) Price1,AVG(ExpertPrice2) as ExpertPrice from [dbo].[PB_BargainExpertPrice] where ExpertPriceNum=2 and ExpertState=3 group by Company_Goods) B
on A.Company_Goods=B.Company_Goods inner join TB_Bargain D on b.Company_Goods=d.BidId inner join  (select Company_Goods,count(1) Num1 from PB_BargainExpertPrice where ExpertState=3 group by Company_Goods) C on A.Company_Goods=C.Company_Goods where C.Num1>=3
union all
select 1 ProjectId,A.Company_Goods,Price1,A.CompanyId_Tb,d.BargainDecPrice,d.BargainEncryptPrice,B.ExpertPrice,0 from TB_GoodsBargin A inner join (
select Company_Goods,AVG(ExpertPrice1) Price1,AVG(ExpertPrice2) as ExpertPrice from [dbo].[PB_BargainExpertPrice] where ExpertPriceNum=2  group by Company_Goods) B
on A.Company_Goods=B.Company_Goods inner join TB_Bargain D on b.Company_Goods=d.BidId inner join  (select Company_Goods,count(1) Num2 from PB_BargainExpertPrice where ExpertState=2 group by Company_Goods) C on A.Company_Goods=C.Company_Goods where C.Num2>=3");
                int reslust = HZ.Data.DbHelperFactory.Create().ExecuteNonQuery(sql);
                if (reslust > 0)
                {
                    HZ.Common.log4netManager.LogInfo("同步到本地第二轮议价数据", string.Format("同步到本地第二轮议价数据成功：操作人：{0}，操作人IP：{1}，操作时间：{2}", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP, DateTime.Now), null);
                    HZ.Web.MessageBox.Show("同步到本地第二轮议价数据成功");
                }
                else
                {
                    HZ.Common.log4netManager.LogInfo("同步到本地第二轮议价数据", string.Format("同步到本地第二轮议价数据失败：操作人：{0}，操作人IP：{1}，操作时间：{2}", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP, DateTime.Now), null);
                    HZ.Web.MessageBox.Show("同步到本地第二轮议价数据失败");
                }
            }
        }
        catch (Exception)
        {
            HZ.Web.MessageBox.Show("同步到本地第二轮议价数据失败");
        }


    }
    /// <summary>
    /// 同步到外网第二轮议价数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button5_Click(object sender, EventArgs e)
    {
        string tradeconnectionstring = ConfigurationManager.AppSettings["OutNetData"].ToString();
        try
        {
            //先同步本地数据信息 第一轮企业议价表TB_Bargain
            string sql = string.Format(@"delete from openrowset({0}.dbo.TB_Bargain2);
                insert into openrowset({0}.dbo.TB_Bargain2)(ProjectId,BidId,ExpertPrice,CompanyId,BargainDecPrice,BargainEncryptPrice,ExpertPrice2)
                select ProjectId,BidId,ExpertPrice,CompanyId,BargainDecPrice,BargainEncryptPrice,ExpertPrice2 from TB_Bargain2; ", tradeconnectionstring);
            int reslust = HZ.Data.DbHelperFactory.Create().ExecuteNonQuery(sql);
            if (reslust > 0)
            {
                HZ.Common.log4netManager.LogInfo("同步到外网第二轮议价数据", string.Format("同步到外网第二轮议价数据成功：操作人：{0}，操作人IP：{1}，操作时间：{2}", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP, DateTime.Now), null);
                HZ.Web.MessageBox.Show("同步到外网第二轮议价数据成功");
            }
            else
            {
                HZ.Common.log4netManager.LogInfo("同步到外网第二轮议价数据", string.Format("同步到外网第二轮议价数据失败：操作人：{0}，操作人IP：{1}，操作时间：{2}", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP, DateTime.Now), null);
                HZ.Web.MessageBox.Show("同步到外网第二轮议价数据失败");
            }
        }
        catch (Exception)
        {
            HZ.Web.MessageBox.Show("同步到外网第二轮议价数据失败");
        }
    }
    /// <summary>
    /// 同步到内网第二轮议价数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button6_Click(object sender, EventArgs e)
    {
        string tradeconnectionstring = ConfigurationManager.AppSettings["OutNetData"].ToString();
        try
        {
            //先同步本地数据信息 第一轮企业议价表TB_Bargain
            string sql = string.Format(@"update TB_Bargain2 
set BargainDecPrice=a.BargainDecPrice,BargainEncryptPrice=a.BargainEncryptPrice,Switch=a.Switch,AcceptDate=a.AcceptDate
from openrowset({0}.dbo.TB_Bargain2)as a inner join TB_Bargain2 b 
on a.BidID=b.BidID ", tradeconnectionstring);
            int reslust = HZ.Data.DbHelperFactory.Create().ExecuteNonQuery(sql);
            if (reslust > 0)
            {
                HZ.Common.log4netManager.LogInfo("同步到内网第二轮议价数据", string.Format("同步到外网第二轮议价数据成功：操作人：{0}，操作人IP：{1}，操作时间：{2}", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP, DateTime.Now), null);
                HZ.Web.MessageBox.Show("同步到外网第二轮议价数据成功");
            }
            else
            {
                HZ.Common.log4netManager.LogInfo("同步到内网第二轮议价数据", string.Format("同步到外网第二轮议价数据失败：操作人：{0}，操作人IP：{1}，操作时间：{2}", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP, DateTime.Now), null);
                HZ.Web.MessageBox.Show("同步到外网第二轮议价数据失败");
            }
        }
        catch (Exception)
        {
            HZ.Web.MessageBox.Show("同步到外网第二轮议价数据失败");
        }
    }
    protected void Button7_Click(object sender, EventArgs e)
    {
        //
        try
        {
            string sql = string.Format(@"
            update PB_BargainExpertPrice set ExpertPriceNum=2 where Company_Goods in(select BidId from TB_Bargain where Switch=2);
            update PB_BargainExpertPrice set ExpertPriceNum=2,ExpertPrice2=ExpertPrice1,ExpertPriceFlag2=1,ExpertPriceTime2=GETDATE(),ExpertState=3 where Company_Goods in(select BidId from TB_Bargain where isnull(Switch,0)=0)");
                int reslust = HZ.Data.DbHelperFactory.Create().ExecuteNonQuery(sql);
                if (reslust > 0)
                {
                    HZ.Common.log4netManager.LogInfo("同步到本地第二轮议价数据", string.Format("同步到本地第二轮议价数据成功：操作人：{0}，操作人IP：{1}，操作时间：{2}", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP, DateTime.Now), null);
                    HZ.Web.MessageBox.Show("同步到本地第二轮议价数据成功");
                }
                else
                {
                    HZ.Common.log4netManager.LogInfo("同步到本地第二轮议价数据", string.Format("同步到本地第二轮议价数据失败：操作人：{0}，操作人IP：{1}，操作时间：{2}", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP, DateTime.Now), null);
                    HZ.Web.MessageBox.Show("同步到本地第二轮议价数据失败");
                }
        }
        catch (Exception)
        {
            
            throw;
        }
    }
}
